package com.ids.actions;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.Map;
import java.util.Set;
import java.util.TreeSet;

import com.ids.bean.UserAttendanceData;
import com.ids.bean.UserInfo;
import com.ids.service.AttendanceService;
import com.ids.util.Constants;
import com.opensymphony.xwork2.ActionContext;
import com.opensymphony.xwork2.ActionSupport;

public class Attendance extends ActionSupport{
	

	private static final long serialVersionUID = 1L;
	private int attendanceTypeId;
	private int dependantAttendanceTypeId;
	private String datepick;
	private int noOfDays;

	public int getNoOfDays() {
		return noOfDays;
	}

	public void setNoOfDays(int noOfDays) {
		this.noOfDays = noOfDays;
	}

	@Override
	public String execute() throws Exception {
		Map<String,Object> session = ActionContext.getContext().getSession();
		UserInfo userInfo = (UserInfo) session.get("userInfo");
		UserAttendanceData userAttendance = null;
		Set<String> resultUserAttendance = null;
		boolean result = validate(getDatepick());
		if(result){
		userAttendance = getUserAttendanceData();
		resultUserAttendance =AttendanceService.getInstance().insertUserAttendanceData(userAttendance, userInfo.getEmpCode() );
		}
		else{
			resultUserAttendance = new TreeSet<String>();
			resultUserAttendance.add(Constants.INVALID);
		}
		if(resultUserAttendance.contains(Constants.FAILURE)){
			session.put(Constants.USER_ATTENDANCE_MESSAGE,"<span style='color:#F00'>Data submission failed");
		}else{
			if (resultUserAttendance.size() >= 2) {
				session.put(Constants.USER_ATTENDANCE_MESSAGE,"<span style='color:#F00'>Data updated and duplicated records are deleted ");
			} else if(resultUserAttendance.contains(Constants.SUCCESS)){
				session.put(Constants.USER_ATTENDANCE_MESSAGE,"<span style='color:#F00'>Data submitted successfully");
			}else if(resultUserAttendance.contains(Constants.UPDATE)){
				session.put(Constants.USER_ATTENDANCE_MESSAGE,"<span style='color:#F00'>Data updated successfully");
			}else if(resultUserAttendance.contains(Constants.INVALID)){
				session.put(Constants.USER_ATTENDANCE_MESSAGE,"<span style='color:#F00'>Date is required");
			}
		}
		return SUCCESS;
	}
		
		private UserAttendanceData getUserAttendanceData(){
			
		Date date = null;
		
		try{
		SimpleDateFormat formatter = new SimpleDateFormat("dd/MM/yyyy");
		date = (Date)formatter.parse(getDatepick()); 
		}catch (ParseException e) {
			e.printStackTrace();
		}
		UserAttendanceData userAttendance = new UserAttendanceData();
		userAttendance.setDate(date);
		userAttendance.setAttendanceTypeId(getAttendanceTypeId());
		userAttendance.setDependantAttendanceTypeId(getDependantAttendanceTypeId());
		userAttendance.setStatusId(Constants.STATUS_PENDING);
		if(getAttendanceTypeId()==Constants.ATTENDANCE_ONE_PL){
			userAttendance.setNoOfDays(getNoOfDays());	
		}
		return userAttendance;
	}
		public int getAttendanceTypeId() {
			return attendanceTypeId;
		}
		public void setAttendanceTypeId(int attendanceTypeId) {
			this.attendanceTypeId = attendanceTypeId;
		}
		public String getDatepick() {
			return datepick;
		}
		public void setDatepick(String datepick) {
			this.datepick = datepick;
		}
		public int getDependantAttendanceTypeId() {
			return dependantAttendanceTypeId;
		}
				public void setDependantAttendanceTypeId(int dependantAttendanceTypeId) {
			this.dependantAttendanceTypeId = dependantAttendanceTypeId;
		}
				
		private boolean validate (String date){
			boolean result= true;
//			int MILLIS_IN_DAY = 1000 * 60 * 60 * 24;
//			Date currentDate = new Date();
//			SimpleDateFormat formatter = new SimpleDateFormat("dd/MM/yyyy");
//			String prevDate = formatter.format(currentDate.getTime() - MILLIS_IN_DAY);
//			String currDate = formatter.format(currentDate.getTime());
//			String nextDate = formatter.format(currentDate.getTime() + MILLIS_IN_DAY);
//			  if(date.equals(currDate)||date.equals(prevDate)||date.equals(nextDate)){
//				  result = true;
//			  }
			if(null==date ||"".equals(date)){
				result =false;
			}
			return result;
		}
}
